package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {


    @Autowired
    Account account;

    @Autowired
    Account account1;
    @Autowired
    AccountService accountService;
    @Autowired
    private HttpSession session;
    @Autowired
    OrderService orderService;
    @Autowired
    CatalogService catalogService;

    public Account getAccount() {
        return this.account;
    }

    public String getUsername() {
        return account.getUsername();
    }

    public void setUsername(String username) {
        account.setUsername(username);
    }

    public String getPassword() {
        return account.getPassword();
    }

    //浏览登录页面
    @GetMapping("/signOnForm")
    public String viewSignOnForm(){
        return "account/signOnForm";
    }

    //登出页面
    @GetMapping("/signOff")
    public String signOff(){
        session.setAttribute("account",null);
        session.setAttribute("cart",null);
        return "catalog/main";
    }

    //进入修改密码My Account
    @GetMapping("/viewEditAccount")
    public String viewEditAccount(String username,Model model){
        account = (Account) session.getAttribute("account");
//        account = accountService.getAccount(username);
        model.addAttribute("account",account);
        System.out.println("正准备修改密码");
        return "account/editAccountForm";
    }

    //注册界面
    @GetMapping("/newAccountForm")
    public String viewNewAccountForm(){
        return "account/newAccountForm";
    }
    //编辑用户信息
    @GetMapping("/editAccount")
    public String editAccount(){

        return "account/editAccountForm";
    }

    @GetMapping("/signOn")
    public String signOnForm(Model model)
    {
        model.addAttribute("account",account);
        return "account/SignOnForm";
    }

    //登录判断
    @PostMapping("/signOn")
    public String signOnSubmit(@ModelAttribute Account account, HttpServletRequest request, Model model)
    {

        String password=request.getParameter("password");
        this.account = accountService.getAccount(account.getUsername(),password);
        if (account.getUsername().equals("seller") && account.getPassword().equals("sell")){
            List<Category> categoryList = catalogService.getCategoryList();
            session.setAttribute("categoryList",categoryList);
            return "manage/catalog/manageCategory";
        }
        if(this.account!=null&&account.getUsername().equals("j2ee")&&account.getPassword().equals("2222"))
        {
            session.setAttribute("account",this.account);
            session.setAttribute("flag",1);
            List<Order> orderList = orderService.getOrdersByUsername(this.account.getUsername());
            session.setAttribute("orderList",orderList);
            List<Category> categoryList = catalogService.getCategoryList();
            model.addAttribute("categoryList",categoryList);
            return "catalog/main";
        }
        else {
                //登录失败
                model.addAttribute("msg", "用户名或密码错误");
            }
            return "account/SignOnForm";
        }

    //用户注册
    @PostMapping("/newAccount")
    public String registerNewAccount(String vCode,String username,String password,String firstName, String lastName,String email,String phone,String address1, String address2,String city,String state,String zip,String country,String languagePreference, String favouriteCategoryId,String listOption,String bannerOption,HttpSession session){

        account = (Account) session.getAttribute("account");
        account = null;
        session.setAttribute("account", account);


        //获得输入的验证码值
        String value1=vCode;
        /*获取图片的值*/
        String value2=(String)session.getAttribute("checkcode");
        Boolean isSame = false;
        /*对比两个值（字母不区分大小写）*/
        if(value2.equalsIgnoreCase(value1)){
            isSame = true;
        }
        account1.setUsername(username);
        account1.setPassword(password);
        account1.setFirstName(firstName);
        account1.setLastName(lastName);
        account1.setEmail(email);
        account1.setPhone(phone);
        account1.setAddress1(address1);
        account1.setAddress2(address2);
        account1.setCity(city);
        account1.setState(state);
        account1.setZip(zip);
        account1.setCountry(country);
        account1.setLanguagePreference(languagePreference);
        account1.setFavouriteCategoryId(favouriteCategoryId);
        account1.setListOption(Boolean.parseBoolean(listOption));
        account1.setBannerOption(Boolean.parseBoolean(bannerOption));

        if(isSame){

            accountService.insertAccount(account1);



            return "catalog/main";
        }else{
            session.setAttribute("messageAccount", "Invalid Verification Code.");

            return "account/newAccountForm";
        }
    }


}
